package org.wmq.travelagency02.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wmq.travelagency02.mapper.MessageMapper;
import org.wmq.travelagency02.model.DTO.HistoryMessageQueryDTO;
import org.wmq.travelagency02.model.Entity.Message;
import org.wmq.travelagency02.service.MessageService;

import java.util.List;

@Service
@RequiredArgsConstructor
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
    private final MessageMapper messageMapper;

    @Override
    public Page<Message> getMessages(Long senderId, Long receiverId) {
        return this.getHistoryMessages(new HistoryMessageQueryDTO(senderId, receiverId,1L));
    }

    @Override
    public Page<Message> getHistoryMessages(HistoryMessageQueryDTO historyMessageQueryDTO) {
        return this.lambdaQuery().eq(Message::getSenderId, historyMessageQueryDTO.getSenderId())
                .eq(Message::getReceiverId, historyMessageQueryDTO.getReceiverId())
                .select(Message::getContent)
                .orderByDesc(Message::getCreateTime)
                .page(new Page<>(historyMessageQueryDTO.getPageNo(),10));
    }
}
